package ManHinhChinh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Table;

public class PhieuGiaoHang {

	protected Shell shellPhieuGiaoHang;
	private Text txt_PGH_DC;
	private Text txt_PGH_SDT;
	private String sSelectDonDatHang = "SELECT * FROM DON_DAT_HANG";
	private Text txt_PGH_KH;
	private Combo cbMDDH;
	private Text txt_PGH_NDH;
	private Text txt_PGH_NGHDK;
	private Table table;
	private boolean started = false;
	private Text text;
	
	private TableColumn tblclmnSTT;
	private TableColumn tblclmnMHH;
	private TableColumn tblclmnTenMatHang;
	private TableColumn tblclmnSoLuong;
	private TableColumn tblclmnDonGiaNhap;
	private TableColumn tblclmnThanhTien;
	private TableColumn tblclmnnDVTinh;
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		// canh giua man hinh
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shellPhieuGiaoHang.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shellPhieuGiaoHang.setLocation(x, y);
		
		shellPhieuGiaoHang.open();
		shellPhieuGiaoHang.layout();
		while (!shellPhieuGiaoHang.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		shellPhieuGiaoHang = new Shell();
		shellPhieuGiaoHang.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				if (!started) {
					TaoKetNoi s = new TaoKetNoi();
					Connection con = s.TaoLienKet();

					Statement stm = null;
					ResultSet rs = null;

					try {
						stm = con.createStatement();
						rs = stm.executeQuery(sSelectDonDatHang);
						while (rs.next()) {
							String key1 = rs.getString("MADONDATHANG");
							cbMDDH.add(key1);
							cbMDDH.setData(key1, rs.getString("MAKHACHHANG"));

						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						con.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
				started = true;
			}
		});
		shellPhieuGiaoHang.setSize(742, 485);
		shellPhieuGiaoHang.setText("Phi\u1EBFu Giao H\u00E0ng");
		
		Label lblNewLabel = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNewLabel.setBounds(31, 66, 88, 27);
		lblNewLabel.setText("Kh\u00E1ch H\u00E0ng");
		
		Label lblaCh = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblaCh.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblaCh.setText("\u0110\u1ECBa Ch\u1EC9");
		lblaCh.setBounds(440, 49, 76, 22);
		
		Label lblinThoi = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblinThoi.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblinThoi.setText("\u0110i\u1EC7n Tho\u1EA1i");
		lblinThoi.setBounds(440, 83, 76, 21);
		
		Label lblNgyGiaoHng = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblNgyGiaoHng.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNgyGiaoHng.setText("Ng\u00E0y Giao H\u00E0ng D\u1EF1 ki\u1EBFn");
		lblNgyGiaoHng.setBounds(31, 158, 173, 24);
		
		Label lblNgyGiaoHng_1 = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblNgyGiaoHng_1.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNgyGiaoHng_1.setText("Ng\u00E0y Giao H\u00E0ng Th\u1EF1c T\u1EBF");
		lblNgyGiaoHng_1.setBounds(31, 198, 173, 27);
		
		txt_PGH_DC = new Text(shellPhieuGiaoHang, SWT.BORDER);
		txt_PGH_DC.setEnabled(false);
		txt_PGH_DC.setBounds(534, 49, 173, 21);
		
		txt_PGH_SDT = new Text(shellPhieuGiaoHang, SWT.BORDER);
		txt_PGH_SDT.setEnabled(false);
		txt_PGH_SDT.setBounds(535, 83, 172, 21);
		
		Button btnNewButton = new Button(shellPhieuGiaoHang, SWT.NONE);
		btnNewButton.setBounds(215, 411, 75, 25);
		btnNewButton.setText("L\u01B0u");
		
		 cbMDDH = new Combo(shellPhieuGiaoHang, SWT.NONE | SWT.DROP_DOWN| SWT.READ_ONLY);
		 cbMDDH.addModifyListener(new ModifyListener() {
		 	public void modifyText(ModifyEvent arg0) {
		 		String MDDH = cbMDDH.getText().trim();
		 		String MKH = cbMDDH.getData(MDDH).toString().trim();
		 		TaoKetNoi s = new TaoKetNoi();
				Connection con = s.TaoLienKet();
				
				Statement stm = null, stm1 = null;
				ResultSet rs = null , rs1 = null;
				// Lấy thông tin
				
				try {
					stm = con.createStatement();
					rs = stm.executeQuery("SELECT * FROM [QuanLyMuaBanHangHoa].[dbo].[KHACH_HANG] WHERE MAKHACHHANG =('"+MKH+"')");
					while (rs.next())
					{
						txt_PGH_KH.setText(rs.getString("HOTEN"));
						txt_PGH_DC.setText(rs.getString("DIACHI"));
						txt_PGH_SDT.setText(rs.getString("DIENTHOAI"));
					}
					stm1=con.createStatement();
					rs1 = stm.executeQuery("SELECT * FROM [QuanLyMuaBanHangHoa].[dbo].[DON_DAT_HANG] WHERE MADONDATHANG =('"+MDDH+"')");
					while (rs1.next())
					{
						txt_PGH_NDH.setText(rs1.getString("NGAYDATHANG"));
						txt_PGH_NGHDK.setText(rs1.getString("NGAYGIAOHANGDUKIEN"));
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		 	}
		 	
		 });
		cbMDDH.setBounds(210, 10, 173, 23);
		
		txt_PGH_KH = new Text(shellPhieuGiaoHang, SWT.BORDER);
		txt_PGH_KH.setEnabled(false);
		txt_PGH_KH.setBounds(210, 66, 173, 21);
		
		Label lblNewLabel_1 = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNewLabel_1.setBounds(31, 10, 110, 23);
		lblNewLabel_1.setText("Mã đơn đặt hàng");
		
		DateTime dtNGHDK = new DateTime(shellPhieuGiaoHang, SWT.BORDER);
		dtNGHDK.setBounds(210, 201, 95, 24);
		
		Label lblNewLabel_2 = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblNewLabel_2.setBounds(31, 122, 110, 22);
		lblNewLabel_2.setText("Ng\u00E0y \u0110\u1EB7t H\u00E0ng");
		
		txt_PGH_NDH = new Text(shellPhieuGiaoHang, SWT.BORDER);
		txt_PGH_NDH.setEnabled(false);
		txt_PGH_NDH.setBounds(212, 122, 171, 21);
		
		txt_PGH_NGHDK = new Text(shellPhieuGiaoHang, SWT.BORDER);
		txt_PGH_NGHDK.setEnabled(false);
		txt_PGH_NGHDK.setBounds(211, 158, 172, 21);
		
		table = new Table(shellPhieuGiaoHang, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(25, 259, 682, 146);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		Label lblMPhiuGiao = new Label(shellPhieuGiaoHang, SWT.NONE);
		lblMPhiuGiao.setText("Mã phiếu giao hàng");
		lblMPhiuGiao.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
		lblMPhiuGiao.setBounds(440, 10, 130, 27);
		
		text = new Text(shellPhieuGiaoHang, SWT.BORDER);
		text.setEnabled(false);
		text.setBounds(577, 10, 130, 21);
		
		tblclmnSTT = new TableColumn(table, SWT.CENTER);
		tblclmnSTT.setWidth(90);
		tblclmnSTT.setText("Số thứ tự");

		tblclmnMHH = new TableColumn(table, SWT.CENTER);
		tblclmnMHH.setWidth(100);
		tblclmnMHH.setText("Mã mặt hàng");

		tblclmnTenMatHang = new TableColumn(table, SWT.CENTER);
		tblclmnTenMatHang.setWidth(100);
		tblclmnTenMatHang.setText("Tên mặt hàng");

		tblclmnnDVTinh = new TableColumn(table, SWT.CENTER);
		tblclmnnDVTinh.setWidth(100);
		tblclmnnDVTinh.setText("Đơn vị tính");

		tblclmnSoLuong = new TableColumn(table, SWT.CENTER);
		tblclmnSoLuong.setWidth(100);
		tblclmnSoLuong.setText("Số lượng");

		tblclmnDonGiaNhap = new TableColumn(table, SWT.CENTER);
		tblclmnDonGiaNhap.setWidth(100);
		tblclmnDonGiaNhap.setText("Đơn giá");

		tblclmnThanhTien = new TableColumn(table, SWT.CENTER);
		tblclmnThanhTien.setWidth(100);
		tblclmnThanhTien.setText("Thành tiền");
		

	}
}
